<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="栏目名称" prop="categoryTitle">
        <el-input
          v-model="queryParams.categoryTitle"
          placeholder="请输入栏目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:category:add']"
        >新增</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="categoryList"
              row-key="id"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="栏目编号" align="center" prop="id" />
      <el-table-column label="栏目名称"  prop="categoryTitle":show-overflow-tooltip="true" width="160" />
      <el-table-column label="栏目类型" align="center" prop="categoryType" />
      <el-table-column label="栏目描述" align="center" prop="categoryDescrip" />
      <el-table-column label="栏目关键字" align="center" prop="categoryKeyword" />
<!--      <el-table-column label="栏目路径" align="center" prop="categoryPath" />-->
      <el-table-column label="自定义顺序" align="center" prop="categorySort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:category:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:category:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:category:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改栏目分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="栏目名称" prop="categoryTitle">
              <el-input v-model="form.categoryTitle" placeholder="请输入栏目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="父类栏目" >
                <treeselect
                  v-model="form.categoryParentId"
                  :options="categoryOptions"
                  :normalizer="normalizer"
                  :show-count="true"
                  placeholder="选择父类栏目"
                />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="栏目类型" prop="categoryType">
              <el-select v-model="form.categoryType" placeholder="请选择栏目类型">
                <el-option
                  v-for="dict in categoryTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="栏目属性" prop="categoryFlag">
              <el-input v-model="form.categoryFlag" placeholder="请输入栏目属性" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="栏目描述" prop="categoryDescrip">
          <el-input v-model="form.categoryDescrip" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-row>
          <el-col :span="12" >
            <el-form-item label="自定义顺序" prop="categorySort">
              <el-input-number v-model="form.categorySort" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关键字" prop="categoryKeyword">
              <el-input v-model="form.categoryKeyword" placeholder="请输入栏目关键字" />
            </el-form-item>
          </el-col>
        </el-row>
<!--        TODO 缩略图-->
<!--        <el-form-item label="缩略图" prop="categoryImg">-->
<!--          <el-input v-model="form.categoryImg" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
        <el-form-item label="内容模板" prop="categoryUrl">
          <el-input v-model="form.categoryUrl" placeholder="请输入内容模板" />
        </el-form-item>
        <el-form-item label="列表模板" prop="categoryListUrl">
          <el-input v-model="form.categoryListUrl" placeholder="请输入列表模板" />
        </el-form-item>
        <el-form-item label="自定义链接" prop="categoryDiyUrl">
          <el-input v-model="form.categoryDiyUrl" placeholder="请输入自定义链接" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCategory, getCategory, delCategory, addCategory, updateCategory, exportCategory } from "@/api/system/category";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Category",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 栏目分类树选项
      categoryOptions: [],
      // 栏目分类类型字典
      categoryTypeOptions: [],
      // 总条数
      total: 0,
      // 栏目分类表格数据
      categoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        categoryTitle: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        categoryTitle: [
          { required: true, message: "栏目分类名称不能为空", trigger: "blur" }
        ],
        categorySort: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" }
        ],
        categoryType: [
          { required: true, message: "栏目类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("env_category_type").then(response => {
      this.categoryTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询栏目分类列表 */
    getList() {
      this.loading = true;
      listCategory(this.queryParams).then(response => {
        this.categoryList = this.handleTree(response.data, "id","categoryParentId");
        this.loading = false;
      });
    },
    /** 转换栏目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.categoryTitle,
        children: node.children
      };
    },
    /** 查询栏目下拉树结构 */
    getTreeselect() {
      listCategory().then(response => {
        this.categoryOptions = [];
        const category = { id: 0, categoryTitle: '栏目分类', children: [] };
        category.children = this.handleTree(response.data, "id","categoryParentId");
        this.categoryOptions.push(category);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        categoryTitle: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.categoryParentId = row.id;
      } else {
        this.form.categoryParentId = 0;
      }
      this.open = true;
      this.title = "添加栏目分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      const id = row.id || this.ids
      getCategory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改栏目分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCategory(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCategory(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除栏目分类编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCategory(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有栏目分类数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCategory(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
